package com.tutor.lzm.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.nfc.Tag;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.tutor.lzm.AppSession;
import com.tutor.lzm.R;
import com.tutor.lzm.activity.MainActivity;
import com.tutor.lzm.activity.TeacherDetailsAct;
import com.tutor.lzm.adapter.SingleCourseAdapter;
import com.tutor.lzm.adapter.TeacherAdapter;
import com.tutor.lzm.bean.SubjectBean;
import com.tutor.lzm.bean.TeacherRecommendBean;
import com.tutor.lzm.function.release.EditStuMessageAct;
import com.tutor.lzm.function.release.ReleaseRequirementAct;
import com.tutor.lzm.http.BaseResponse;
import com.tutor.lzm.http.HttpClient;
import com.tutor.lzm.http.request.TeacherListRequest;
import com.tutor.lzm.http.request.TeacherRecommendRequest;
import com.tutor.lzm.http.response.TeacherListResponse;
import com.tutor.lzm.http.response.TeacherRecommendList;
import com.tutor.syslib.Logs;
import com.tutor.syslib.listener.PullUpRefreshImpl;
import com.tutor.syslib.view.MyListView;
import com.tutor.syslib.view.MyScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zzgpc on 2017-8-24.
 * 请家教
 */
public class HireTutorFragment extends BaseFragment {

    @BindView(R.id.popular_recommendation_listView)
    MyListView popularRecommendationListView;
    @BindView(R.id.img_release_tutor)
    ImageView imgReleaseTutor;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.my_scroll_view)
    MyScrollView myScrollView;
    Unbinder unbinder;

    private TeacherAdapter adapter;
    /**
     * 是否展示完整的发布横条
     */
    private boolean isShowRelease = false;

    /**
     * 加载位置坐标
     */
    private int currentPage = 0;
    /**
     * 热门推荐老师
     */
    private List<TeacherRecommendBean> recommendBeanList = new ArrayList<>();

    @Override
    public int getFragmentLayout() {
        return R.layout.fragment_hire_tutor;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView(Bundle bundle) {
        super.initView(bundle);
        initListView();
        myScrollView.setPullUpRefreshImpl(pullUpRefresh);

    }

    @Override
    protected void initData(Bundle bundle) {
        super.initData(bundle);

        getTeacherList();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {// 不在最前端界面显示
        } else {// 重新显示到最前端中
            List<SubjectBean> subjectList = AppSession.getInstance().getSubjectList();
            if (subjectList == null) {
                return;
            }
            initSubject(subjectList);
        }
    }

    /**
     * 加载热门老师listView
     */
    private void initListView() {
        MyListView myListView = (MyListView) findViewById(R.id.popular_recommendation_listView);
        adapter = new TeacherAdapter(getContext(), recommendBeanList);
        myListView.setAdapter(adapter);
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TeacherRecommendBean bean = recommendBeanList.get(position);
                TeacherDetailsAct.start(getContext(), bean.teacherId);
            }
        });
    }

    /**
     * 初始化学科
     */
    private void initSubject(List<SubjectBean> subjectList) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        SingleCourseAdapter singleCourseAdapter = new SingleCourseAdapter(getContext(), subjectList);
        recyclerView.setAdapter(singleCourseAdapter);

    }

    /**
     * 获取老师列表
     */
    private void getTeacherList() {
        final TeacherListRequest request = new TeacherListRequest();
        request.start = currentPage;
        request.userId = AppSession.getInstance().getUserId();
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                Logs.e("mainfragment", "response");
                try {
                    TeacherListResponse recommendList = (TeacherListResponse) response;
                    List<TeacherRecommendBean> adResponse = recommendList.teacherList;
                    recommendBeanList.addAll(adResponse);
                    adapter.notifyDataSetChanged();
                    if (adResponse.size() == request.limit || adResponse.size() != 0) {
                        currentPage++;
                    }

                } catch (Exception e) {
                    Logs.e(tag, e.getMessage());
                }

                return;

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d("MainFragment", "getRecommendSubject    code" + code + "      msg=" + msg);
            }
        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view

        contentView = inflater.inflate(getFragmentLayout(), container, false);
        unbinder = ButterKnife.bind(this, contentView);
        super.onCreateView(inflater, container, savedInstanceState);
        return contentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //解绑ButterKnife
        unbinder.unbind();
    }

    @OnClick({ R.id.img_release_tutor})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_release_tutor:
                //发布家教
                float removing = getResources().getDimension(R.dimen.release_tutor_move);
                if (!isShowRelease) {
                    ObjectAnimator moveIn = ObjectAnimator.ofFloat(imgReleaseTutor, "translationX", 0f, 0f, -removing, -removing);
//                ObjectAnimator rotate = ObjectAnimator.ofFloat(imgRelaseTutor, "rotation", 0f, 360f);
//                ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(imgRelaseTutor, "alpha", 1f, 0f, 1f);
//                AnimatorSet animSet = new AnimatorSet();
//                animSet.play(rotate).with(fadeInOut).after(moveIn);
                    moveIn.setDuration(1000);
                    moveIn.start();
                    isShowRelease = true;
                } else {
                    ObjectAnimator moveIn = ObjectAnimator.ofFloat(imgReleaseTutor, "translationX", 0f);
                    moveIn.setDuration(1000);
                    moveIn.start();
                    isShowRelease = false;
//                    ReleaseRequirementAct.start(getContext());
                    //添加学生借本信息
                    EditStuMessageAct.start(getContext());
                }

                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    /**
     * MyScrollView下拉刷新
     */
    private PullUpRefreshImpl pullUpRefresh = new PullUpRefreshImpl() {
        @Override
        public void setOnPullUpRefresh() {
            int size = recommendBeanList.size();
            if (size % 10 == 0) {
                getTeacherList();
            }

        }
    };
}
